package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.MySignInBean;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;

/**
 * Created by zcy on 2016/8/20.
 */
public class MySignInAdapter extends BaseAdapter {

    private Activity activity;
    private ViewHolder viewHolder;
    List<MySignInBean> data;
    private LinearLayout linearLayout;



    public MySignInAdapter(Activity activity, List<MySignInBean> data) {
        this.activity = activity;
        this.data = data;
    }


    /**
     * 刷新数据
     *
     * @param data
     */
    public void fresh(List<MySignInBean> data,boolean isAddAll) {

        if (isAddAll){
            this.data.addAll(data);
        }else {
            this.data = data;
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        if (data != null && data.size() > 0){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {

        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        View view;

        if (convertView == null) {
            view = LayoutInflater.from(activity).inflate(R.layout.item_my_sign_in_listview, null);
            linearLayout = (LinearLayout) view.findViewById(R.id.my_sign);

            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.personal_center_my_sign_over_button);
            viewHolder.title = (TextView) view.findViewById(R.id.personal_center_my_sign_title);

            view.setTag(viewHolder);//将viewHolder储存在view中

        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data != null && !data.toString().equals("[]")){

            viewHolder.title.setText(data.get(i).getName());

            GetVolleyHttp.getInstance(activity).setImageLoader(viewHolder.imageView,
                    R.drawable.activity_publish_news_photo,
                    R.drawable.activity_publish_news_photo, data.get(i).getImageUrl());
        }

        return view;

    }

    private class ViewHolder {
        ImageView imageView;
        TextView title;
    }
}
